﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eTruck.Helpers;
namespace eTruck.Views.Forms
{
    public partial class FormQueue : System.Web.UI.Page
    {
        public string Module, ProcessID, PreProcessID,ModuleName;
        protected void Page_Load(object sender, EventArgs e)
        {
            Module = "";
            DataSet ds;
            if (Request.QueryString["Module"] != null)
            {
                try
                {

                    Module = Request.QueryString["Module"].ToString();
                    eTruck.BLL.Modules bll_Modules = new BLL.Modules();
                    eTruck.Model.Modules model_Module = bll_Modules.GetModel(Convert.ToInt32(Module));
                    ModuleName = model_Module.ModuleName;
                    eTruck.BLL.Process bll_Process = new BLL.Process();
                    ds = bll_Process.GetList(" ProcessName='" + ModuleName + "'");
                    ProcessID = ds.Tables[0].Rows[0][0].ToString();
                }
                catch
                {
                }

            }
            else
            {
                string url = "../../Views/Users/Login.aspx";
                MessageBoxHelper.MessageBox(Page, "请重新登陆", url);
            }
            PreProcessID = "";
            eTruck.BLL.Route bll_Route = new BLL.Route();
            ds = bll_Route.GetList(" NextProcessID='" + ProcessID + "'");
            if (ds != null)
            {
                if (ds.Tables.Count != 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        PreProcessID = PreProcessID + ds.Tables[0].Rows[i][1].ToString() + ",";
                    }
                }
            }
            if (PreProcessID != "")
            {
                PreProcessID = PreProcessID.Substring(0, PreProcessID.Length - 1);
            }
            if (ModuleName == "车辆预约进厂")
            {
                PreProcessID = ProcessID;
            }
            if ((ModuleName != "车辆预约进厂") && (ModuleName != "车辆准备进厂"))
            {

                btnAdd.Visible = false;
            }
            else
            {
                
                gridView.Columns[10].Visible = true;
            }
            if (!IsPostBack)
            {

                Show("");

            }
        }
        protected void Show(string KeyWord)
        {
            eTruck.BLL.Form bll_Form = new BLL.Form();
            DataSet ds = bll_Form.GetInMyQueue(PreProcessID, KeyWord);
            if (ExistsData(ds))
            {
                gridView.DataSource = ds.Tables[0];
                gridView.DataBind();
            }
            else
            {
                ShowEmpty();
            }
        }
        protected void ShowEmpty()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FormID");
            dt.Columns.Add("Car_NO");
            dt.Columns.Add("CorporationName");
            dt.Columns.Add("CertificationNO");
            dt.Columns.Add("DriverName");
            dt.Columns.Add("FormType");
            dt.Columns.Add("CardID");
            dt.Columns.Add("Contact");
            dt.Columns.Add("OrderNumber");
            dt.Columns.Add("Reservation_In_Time");
            dt.Rows.Add(dt.NewRow());
            gridView.DataSource = dt;
            gridView.DataBind();
            gridView.Columns[9].Visible = false;
            gridView.Columns[10].Visible = false;
        }
        protected Boolean ExistsData(DataSet ds)
        {
            if (ds == null)
            {
                return false;
            }
            if (ds.Tables.Count == 0)
            {
                return false;
            }
            if (ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            return true;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Show(txtKeyword.Text);
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            Response.Redirect("Form.aspx?ProcessID=" + ProcessID + "&Action=New");            
           
        }

        protected void gridView_PageIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index=int.Parse(e.CommandArgument.ToString());
            string FormID = gridView.DataKeys[index].Value.ToString();
            if (e.CommandName == "Detail")
            {

                Response.Redirect("Form.aspx?ProcessID=" + ProcessID + "&Action=Detail&FormID=" + FormID);
            }
            if (e.CommandName == "Delete")
            {
                BLL.FormHistory bll_History = new BLL.FormHistory();
                bll_History.Delete(Convert.ToInt32(FormID));
                BLL.Form bll_Form = new BLL.Form();
                bll_Form.Delete(Convert.ToInt32(FormID));
                Show(txtKeyword.Text);
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}